<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/20
 * Time: 上午11:27
 */
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller
{
    //客户登陆
    public function Login()
    {
        if(IS_POST){
            //接收用户名
            $username = I('post.username',0);
            if(empty($username)){
                $this->error('非法操作！');
            }
            $user_length = strlen($username);
            if($user_length < 3 || $user_length > 20){
                $this->error('别胡搞！');
            }
            //接收密码
            $password = I('post.password','');
            if(empty($password)){
                $this->error('非法操作！');
            }
            $member = M('member');
            $member_info = $member->where(['account'=>$username])->field('id,account,nick,password,salt')->find();
            if(!$member_info){
                $this->error('该用户不存在！');
            }
            if(md5($password.$member_info['salt']) != $member_info['password']){
                $this->error('账号和密码错误！');
            }
            unset($member_info['password'],$member_info['salt']);
            session(C('FRONT_SESSION'),$member_info);
            //获取是否记住用户名
            $remember = I('post.remember',0);
            if(!empty($remember)){
                $_SESSION['username'] = $username;
            }
            $this->success('登陆成功','/');
            exit;
        }else{
            if(!empty(session(C('FRONT_SESSION')))){
                $this->redirect('/');
            }
            $this->assign('title','TD商城登陆');
            $this->display();
        }
    }

    //客户注册
    public function register()
    {
        if(IS_POST){
            //接收是否接受用户协议
            $agreement = I('post.agreement',0);
            if(empty($agreement)){
                $this->error('必须接受用户协议才能注册！');
            }
            //接收用户名
            $username = I('post.username',0);
            if(empty($username)){
                $this->error('非法操作！');
            }
            $user_length = strlen($username);
            if($user_length < 3 || $user_length > 20){
                $this->error('用户名格式错误！');
            }
            //接收密码
            $password = I('post.password','');
            if(empty($password)){
                $this->error('非法操作！');
            }
            //生成盐
            $salt = getRandomNumber();
            $member = M('member');
            $member->startTrans();
            $member_id = $member->add(['account'=>$username,'password'=>md5($password.$salt),'salt'=>$salt,'reg_time'=>time()]);
            if($member_id && M('member_info')->add(['mid'=>$member_id,'name'=>$username])){
                $member->commit();
                $member_info = $member->where(['id'=>$member_id])->field('id,account,nick')->find();
                session(C('FRONT_SESSION'),$member_info);
                $this->success('注册成功！','/');
                exit;
            }
            $member->rollback();
            $this->error('注册失败！');
        }else{
            $this->assign('title','TD商城注册');
            $this->display();
        }
    }

    //检查注册的用户名
    public function check_user(){
        if(IS_GET){
            $username = I('get.username');
            $member = M('Member');
            if($member->where(['account'=>$username])->getField('id') === NULL){
                $this->ajaxReturn(true);
            }
            $this->ajaxReturn(false);
        }else{
            $this->ajaxReturn(false);
        }
    }

    //退出登录
    public function login_out()
    {
        session(C('FRONT_SESSION'),null);
        $this->redirect('Login/login');
    }
}